﻿using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.Sign;
using MedicalPCTerminal.Read.API.Dtos;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.Sign
{

    public class GetpatientCommandHandler : IRequestHandler<GetpatientCommand, APIResult<List<GetPatientByDoctorIdDTO>>>
    {
        private readonly SqlSugarContext sqlSugar;

        public GetpatientCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }

        public async Task<APIResult<List<GetPatientByDoctorIdDTO>>> Handle(GetpatientCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<GetPatientByDoctorIdDTO>> result = new APIResult<List<GetPatientByDoctorIdDTO>>();
            try
            {
                var query = sqlSugar.db.Queryable<NCDSign, ResidentFiles>((ncd, resident) => ncd.Patient_Id == resident.ResidentFiles_Id)
                   .Where((ncd, resident) => ncd.Doctor == request.Doctor && ncd.Sign_Status == 1)
                   .Select((ncd, resident) => new GetPatientByDoctorIdDTO
                   {
                       Patient_Id=ncd.Patient_Id,
                       ResidentFiles_Name=resident.ResidentFiles_Name,
                       Sex=resident.Sex,
                       Age = resident.Age
                   });

                var data = await query.ToListAsync(cancellationToken);

                result.data = data;
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
            }
            catch (Exception ex)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }


            return await  Task.FromResult(result);
        }
    }
}
